package cn.toys.modular.service.impl;

import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.toys.base.pojo.page.LayuiPageFactory;
import cn.toys.modular.entity.Product;
import cn.toys.modular.enums.ProductStatus;
import cn.toys.modular.mapper.ProductMapper;
import cn.toys.modular.service.IProductService;
import cn.toys.sys.core.constant.factory.ConstantFactory;
import cn.toys.sys.modular.system.service.DeptService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 产品表 服务实现类
 * </p>
 *
 * @author toys
 * @since 2021-02-26
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private DeptService deptService;

    @Override
    public Page<Map<String, Object>> selectProducts(String condition) {
        Page<Map<String, Object>> page = LayuiPageFactory.defaultPage();
        Page<Map<String, Object>> pageResult = this.baseMapper.selectProducts(page, condition);
        List<Map<String, Object>> records = pageResult.getRecords();
        List<Map<String, Object>> newRecords = new ArrayList<>();
        for (Map<String, Object> map : records) {
            Map<String, Object> resMap = new HashMap<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                resMap.put(key,value);
                if("tenant_id".equals(key)){
                    //拼接得出父级名称，例 A -》 公司：xx：xx
                   String tenantName =  deptService.getTenantFullName(value.toString());
                   resMap.put("tenantName",tenantName);
                }
                if("is_flag".equals(key)){
                    String description = ProductStatus.getDescription(value.toString());
                    resMap.put("productStatus",description);
                }
            }
            newRecords.add(resMap);
        }
        pageResult.setRecords(newRecords);
        return pageResult;
    }
}
